package com.zz.demo2;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;


import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class GZIPUtil {
    /**
     *
     * @Title: pack
     * @Description: 将一组文件打成tar包
     * @param sources 要打包的原文件数组
     * @param target 打包后的文件
     * @return File    返回打包后的文件
     * @throws
     */
    public static File pack(List<File> sources, File target) throws FileNotFoundException {
        FileOutputStream out = null;
        out = new FileOutputStream(target);
        TarArchiveOutputStream os = new TarArchiveOutputStream(out);
        for (File file : sources) {
            try {
                os.putArchiveEntry(new TarArchiveEntry(file));
                IOUtils.copy(new FileInputStream(file), os);
                os.closeArchiveEntry();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(os != null) {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return target;
    }

    /**
     *
     * @Title: compress
     * @Description: 将文件用gzip压缩
     * @param  source 需要压缩的文件
     * @return File    返回压缩后的文件
     * @throws
     */
    public static File compress(File source) {
        File target = new File(source.getName() + ".gz");
        FileInputStream in = null;
        GZIPOutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new GZIPOutputStream(new FileOutputStream(target));
            byte[] array = new byte[1024];
            int number = -1;
            while((number = in.read(array, 0, array.length)) != -1) {
                out.write(array, 0, number);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }

            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
        return target;
    }

    public static void generateZip(OutputStream os, List<File> files) throws Exception {
        ZipOutputStream out = null; try { byte[] buffer = new byte[1024];
            //生成的ZIP文件名为Demo.zip
             out = new ZipOutputStream(os);
            // 需要同时下载的两个文件result.txt source.txt
            for (File file : files) {
                FileInputStream fis = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(file.getName()));
                int len;
                //读入需要下载的文件的内容，打包到zip文件
                while ((len = fis.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.flush();
                out.closeEntry();
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        /*File[] sources = new File[] {new File("task.xml"), new File("app.properties")};
        File target = new File("release_package.tar");
        compress(pack(sources, target));*/
        File file = new File("/opt/log");
        File[] files = file.listFiles();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //huoquqianyitianriqi
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        ca.add(Calendar.DATE,-1);
        String format = dateFormat.format(ca.getTime());
        //String format = dateFormat.format(new Date());
        List<File> file1 = new ArrayList<>();
        for (File filel:files) {
            String[] split = filel.getName().split("\\.");
            //System.out.println(split[2]);
            if (split.length==4 && format.equals(split[2])) {
                file1.add(filel);
            }
        }
        //File target = new File(format+".tar");
        //File pack = pack(file1, target);
        System.out.println(file1);
        FileOutputStream fos = new FileOutputStream(new File("/opt/log/2018-01-06.tar.gz"));
        generateZip(fos,file1);
        /*System.out.println(pack.length());
        FileOutputStream os = null;
        InputStream inputStream= new FileInputStream(pack);
        try{
        byte[] array = new byte[2048];
        int len;
        // 输出的文件流保存到本地文件
            System.out.println(pack.getName());

        if (!pack.exists()) {
            pack.mkdirs();
        }
        os = new FileOutputStream("/opt/log/" + File.separator + pack.getName());
        // 开始读取
        while ((len = inputStream.read(array)) != -1) {
            os.write(array, 0, len);
        }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕，关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        }
}
